#!/bin/bash

# 检查系统类型
if [[ "$(uname)" == "Linux" ]]; then
    # 检查发行版
    if command -v apt-get &> /dev/null; then
        # Ubuntu或Debian
        sudo apt-get update
        sudo apt-get install -y dialog
        sudo apt-get install -y curl
    elif command -v yum &> /dev/null; then
        # CentOS或RHEL
        sudo yum install -y dialog
        sudo yum install -y curl
    else
        echo "Unsupported Linux distribution"
        exit 1
    fi
else
    echo "Unsupported operating system"
    exit 1
fi

# 弹出菜单
choice=$(dialog --backtitle "白菜-签名API" \
    --title "请选择签名提供者" \
    --menu "请选择:" 15 60 2 \
    "1" "咕咕咕" \
    "2" "白菜" 3>&1 1>&2 2>&3)

# 处理用户选择
case $choice in
    1)
        # 弹出子菜单
        sub_choice=$(dialog --backtitle "白菜-签名API" \
            --title "请选择版本号" \
            --menu "请选择:" 15 60 5 \
            "1" "8.9.78" \
            "2" "8.9.85" \
            "3" "8.9.88" \
            "4" "8.9.90" \
            "5" "8.9.93" 3>&1 1>&2 2>&3)

        # 处理子菜单选择
        case $sub_choice in
            1)
                url="http://47.108.180.154:8978/sign?key=114514"
                ;;
            2)
                url="http://47.108.180.154:8985/sign?key=114514"
                ;;
            3)
                url="http://47.108.180.154:8988/sign?key=114514"
                ;;
            4)
                url="http://47.108.180.154:8990/sign?key=114514"
                ;;
            5)
                url="http://47.108.180.154:8993/sign?key=114514"
                ;;
            *)
                echo "你在选什么？"
                exit 1
                ;;
        esac

        # 检测URL是否可访问
        if curl --output /dev/null --silent --head --fail "$url"; then
            dialog --backtitle "白菜-签名API" \
                --title "签名可用" \
                --yesno "是否填入?" 15 60
            response=$?
            if [ $response -eq 0 ]; then
                # 写入配置文件
                echo "sign_api_addr: $url" | sed '33s/.*/&'$'\n/' > TRSS_AllBot/Miao-Yunzai/config/config/bot.yaml
            fi
        else
            dialog --backtitle "白菜-签名API" \
                --title "签名不可用" \
                --msgbox "签名提供者不可访问" 15 60
        fi
        ;;
    2)
    
        echo "白菜选项"
        ;;
    *)
        echo "你在选什么？"
        exit 1
        ;;
esac
